package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.R;
import com.entity.Category;
import com.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //增加分类
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功");
    }
    //分页查询
    @GetMapping("/page")
    public R<Page> selectByPage(int page, int pageSize){
        Page pageInfo=new Page(page,pageSize);//创建分页构造器
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();//创建条件构造器（没有条件就service调用方法就不加这个当参数），这里可以设置个排序的
        lqw.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,lqw);//不用设置返回结果，会自动进行封装
        return R.success(pageInfo);//将封装好的数据返回给前端
    }
    //删除一个类
    @DeleteMapping
    public R<String> deleteById(Long id){
        log.info("删除的id是："+id);
        categoryService.remove(id);
        return R.success("删除成功");
    }
    //分类更新操作，数据回显前端已做
    @PutMapping
    public R<String> updateById(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("成功");
    }
    //获取菜品类型集合
    @GetMapping("/list")
    public R<List<Category>> dishList(Category category){
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.eq(category.getType()!=null,Category::getType,category.getType());//如果传递的参数不为空，则匹配与该参数相同的集合
        lqw.orderByAsc(Category::getSort).orderByAsc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }
}
